//
//  MYPictureSelectedController.m
//  MYChatting
//
//  Created by mac on 16/8/30.
//  Copyright © 2016年 lhf. All rights reserved.
//

#import "MYPictureSelectedController.h"
#import "MYPictureSelectedCell.h"

static CGFloat rowCount = 5;
static CGFloat margin = 5;
static CGFloat maxImageCount = 9;


@interface MYPictureSelectedController ()<MYPictureSelectedCellDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>




@end

@implementation MYPictureSelectedController

static NSString * const reuseIdentifier = @"Cell";

#pragma  MYPictureSelectedCell代理方法的实现
- (void)userWillChosePictureWithCell:(MYPictureSelectedCell *)cell{
    
#warning 使用return结束该方法  十分的妙
    if (cell.image != nil) {
        return;
    }
    //获得图片  在图片选择器中选择
    UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
    [self presentViewController:pickerController animated:YES completion:nil];
    
    //设置UIImagePickerControllerDelegate的代理
    pickerController.delegate = self;
}


- (void)userWillDeletePictureWithCell:(MYPictureSelectedCell *)cell{
    NSIndexPath *indexpath = [self.collectionView indexPathForCell:cell];
    [self.imageList removeObjectAtIndex:indexpath.item];
    [self.collectionView reloadData];
}



- (void)imagePickerController:(nonnull UIImagePickerController *)picker didFinishPickingImage:(nonnull UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo{
    [self.imageList addObject:image];
    [self.collectionView reloadData];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (instancetype)init{
    CGFloat itemW = ( kScreenWidth - (rowCount + 1) * margin ) / rowCount;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(itemW, itemW);
    flowLayout.minimumInteritemSpacing= margin;
    flowLayout.minimumLineSpacing = margin;
    flowLayout.sectionInset = UIEdgeInsetsMake(margin, margin, 0, margin);
    
    return [super initWithCollectionViewLayout:flowLayout];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Register cell classes
   self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionView registerClass:[MYPictureSelectedCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
}



#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    CGFloat delta = self.imageList.count == maxImageCount ? 0 : 1;
    return self.imageList.count + delta;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MYPictureSelectedCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    //设置MYPictureSelectedCell的代理
    cell.delegate = self;
    
    if (indexPath.item == self.imageList.count) {
      
        cell.image = nil;
    }else{
        cell.image = self.imageList[indexPath.item];
       
    }
    
   
    return cell;
}


- (NSMutableArray *)imageList{
    if (_imageList == nil) {
        _imageList = [NSMutableArray array];
    }
    return _imageList;
}



@end
